package com.vitetest.api.controller;

import com.vitetest.api.dto.ApiResponse;
import com.vitetest.api.dto.LoginRequest;
import com.vitetest.api.dto.LoginResponse;
import com.vitetest.api.service.UserService;
import javax.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * 认证控制器
 * 
 * @author ViteTest Team
 * @version 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("/api/auth")
@RequiredArgsConstructor
@CrossOrigin(origins = "*")
public class AuthController {

    private final UserService userService;

    /**
     * 用户登录
     * 
     * @param loginRequest 登录请求
     * @return 登录响应
     */
    @PostMapping("/login")
    public ResponseEntity<ApiResponse<LoginResponse>> login(@Valid @RequestBody LoginRequest loginRequest) {
        try {
            log.info("收到登录请求: {}", loginRequest.getUsername());
            
            LoginResponse response = userService.authenticateUser(
                    loginRequest.getUsername(), 
                    loginRequest.getPassword()
            );
            
            return ResponseEntity.ok(ApiResponse.success("登录成功", response));
        } catch (Exception e) {
            log.error("登录失败: {}", e.getMessage());
            return ResponseEntity.badRequest()
                    .body(ApiResponse.error("登录失败: " + e.getMessage()));
        }
    }

    /**
     * 刷新令牌
     * 
     * @param refreshToken 刷新令牌
     * @return 新的访问令牌
     */
    @PostMapping("/refresh")
    public ResponseEntity<ApiResponse<LoginResponse>> refreshToken(@RequestParam String refreshToken) {
        try {
            log.info("收到令牌刷新请求");
            
            LoginResponse response = userService.refreshToken(refreshToken);
            
            return ResponseEntity.ok(ApiResponse.success("令牌刷新成功", response));
        } catch (Exception e) {
            log.error("令牌刷新失败: {}", e.getMessage());
            return ResponseEntity.badRequest()
                    .body(ApiResponse.error("令牌刷新失败: " + e.getMessage()));
        }
    }

    /**
     * 健康检查
     * 
     * @return 服务状态
     */
    @GetMapping("/health")
    public ResponseEntity<ApiResponse<String>> health() {
        return ResponseEntity.ok(ApiResponse.success("ViteTest API 服务正常运行"));
    }

    /**
     * 获取API信息
     * 
     * @return API信息
     */
    @GetMapping("/info")
    public ResponseEntity<ApiResponse<Object>> getApiInfo() {
        Object info = new Object() {
            public final String name = "ViteTest API";
            public final String version = "1.0.0";
            public final String description = "Spring Boot REST API for ViteTest Login";
            public final String[] endpoints = {
                "POST /api/auth/login - 用户登录",
                "POST /api/auth/refresh - 刷新令牌",
                "GET /api/auth/health - 健康检查",
                "GET /api/auth/info - API信息"
            };
        };
        
        return ResponseEntity.ok(ApiResponse.success(info));
    }
} 